package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appNoticeDao")
public class AppNoticeDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppNotice queryWithKeys(String id) throws SQLException {
		AppNotice notice = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM app_notice_show WHERE record_key=?");
			ps.setString(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				notice = new AppNotice();
				notice.setRecordKey(rs.getString("record_key"));
				notice.setAppId(rs.getString("APP_ID"));
				notice.setNoticeTitle(rs.getString("notice_title"));
				notice.setNoticeContent(rs.getString("notice_content"));
				notice.setNoticeDate(rs.getString("notice_date"));
				notice.setNoticeEndDate(rs.getString("notice_end_date"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryNoticeWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return notice;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppNotice notice) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		// ResultSet rs=null;
		int flag;
		try {
			String sql = "insert into app_notice_show values(?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, notice.getRecordKey());
			ps.setString(2, notice.getAppId());
			ps.setString(3, notice.getNoticeTitle());
			ps.setString(4, notice.getNoticeContent());
			ps.setString(5, notice.getNoticeDate());
			ps.setString(6, notice.getNoticeEndDate());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insertNotice is Wrong!" + e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppNotice notice) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update app_notice_show set "
					+ "notice_title=? , notice_content=? , notice_date=? , "
					+ "notice_end_date=? where record_key=? and APP_ID=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, notice.getNoticeTitle());
			ps.setString(2, notice.getNoticeContent());
			ps.setString(3, notice.getNoticeDate());
			ps.setString(4, notice.getNoticeEndDate());
			ps.setString(5, notice.getRecordKey());
			ps.setString(6, notice.getAppId());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("updateNoticeWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String recordKey) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from app_notice_show where record_key=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, recordKey);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("deleteNoticeWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
